package com.wgchao.identityrightsmanagement.query.vo;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAccount;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysResourceMenu;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAccountRepository;
import com.wgchao.identityrightsmanagement.infrastructure.mapstruct.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @ClassName: MenuDetailVoConvert
 * @author: wgchao
 * @createTime: 2020/6/28 10:42 AM
 * @Description: TODO
 */
@Component
public class MenuDetailVoConvert{
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private SysBaseAccountRepository sysBaseAccountRepository;

    public MenuDetailVo convert(SysResourceMenu sysResourceMenu){
        MenuDetailVo menuDetailVo = menuMapper.toT(sysResourceMenu);
        if(!StringUtils.isEmpty(sysResourceMenu.getCreator())){
            SysBaseAccount creator = sysBaseAccountRepository.findFirstByUsername(sysResourceMenu.getCreator());
            menuDetailVo.setCreatorName(creator == null ? null : creator.getName());
        }
        if(!StringUtils.isEmpty(sysResourceMenu.getLastUpdator())){
            SysBaseAccount updator = sysBaseAccountRepository.findFirstByUsername(sysResourceMenu.getLastUpdator());
            menuDetailVo.setLastUpdatorName(updator == null ? null : updator.getName());
        }
        return menuDetailVo;
    }
}
